<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>APP</title>
    <link rel="stylesheet" href="../css/api.css" />
    <link rel="stylesheet" href="../css/aui.css" />
    <link rel="stylesheet" href="../css/aui-flex.css" />
    <link rel="stylesheet" href="../css/common.css" />
    <link rel="stylesheet" href="../css/login.css">
    <link rel="stylesheet" href="../css/font/iconfont.css">
</head>
<body>
  <div id="wrap">
    <header class="aui-bar aui-bar-nav aui-bg-danger">
      <div class="aui-pull-left aui-btn" onclick="closeWin();">
        <span class="aui-iconfont aui-icon-left"></span>
      </div>
      <div class="aui-title aui-text-white">注册</div>
    </header>

    <div id="step1">
      <div class="aui-flex-col aui-flex-middle aui-padded-10 aui-bg-white aui-margin-t-10">
        <label class="aui-margin-r-10">手机号</label>
        <div class="aui-flex-auto aui-list-item-input">
          <input
            id="oldTele"
            class="aui-font-size-14"
            type="tele"
            placeholder="请输入手机号"
          >
        </div>
      </div>

      <div class="aui-padded-l-10 aui-padded-r-10 aui-bg-white">
        <div class="aui-flex-col aui-flex-middle  aui-margin-t-10" style="position: relative;">
          <label class="aui-margin-r-10">验证码</label>
          <div class="aui-flex-auto aui-list-item-input">
            <input
              id="code"
              class="aui-font-size-14"
              type="text"
              placeholder="请输入短信验证码"
            >
          </div>
          <span class="aui-font-size-12 aui-btn aui-bg-danger aui-text-white tele-code-btn" onclick="getRegCode()">获取验证码</span>
        </div>
        <div class="aui-hr"></div>
        <p class="aui-font-size-12 aui-padded-t-10 aui-padded-b-10">
          点击获取验证码您可收到验证短信，五分钟内有效
        </p>
      </div>

      <div class="aui-flex-col aui-flex-middle aui-padded-l-10 aui-padded-r-10 aui-bg-white aui-margin-t-10">
        <label class="aui-margin-r-10">密码</label>
        <div class="aui-flex-auto aui-list-item-input">
          <input
            id="pwd1"
            class="aui-font-size-14"
            type="text"
            placeholder="请输入密码"
          >
        </div>
      </div>

      <div class="aui-flex-col aui-flex-middle aui-padded-l-10 aui-padded-r-10 aui-bg-white aui-margin-t-10">
        <label class="aui-margin-r-10">确认密码</label>
        <div class="aui-flex-auto aui-list-item-input">
          <input
            id="pwd2"
            class="aui-font-size-14"
            type="text"
            placeholder="请再次输入密码"
          >
        </div>
      </div>

      <div class="aui-padded-15 aui-margin-t-25">
        <div
          class="aui-btn aui-btn-block aui-bg-danger aui-text-white"
          onclick="next()"
        >下一步</div>
      </div>
    </div>
    <div id="step2" class="plate">
      <div class="aui-flex-col aui-flex-middle aui-padded-l-10 aui-padded-r-10 aui-bg-white aui-margin-t-10">
        <label class="aui-margin-r-10">姓名</label>
        <div class="aui-flex-auto aui-list-item-input">
          <input
            id="name"
            class="aui-font-size-14"
            type="text"
            placeholder="请输入姓名"
          >
        </div>
      </div>

      <div class="aui-flex-col aui-flex-middle aui-padded-l-10 aui-padded-r-10 aui-bg-white aui-margin-t-10">
        <label class="aui-margin-r-10">身份证号</label>
        <div class="aui-flex-auto aui-list-item-input">
          <input
            id="idCard"
            class="aui-font-size-14"
            type="text"
            placeholder="请输入身份证号"
          >
        </div>
      </div>

      <div class="aui-padded-15 aui-margin-t-25">
        <div
          class="aui-btn aui-btn-block aui-bg-danger aui-text-white"
          onclick="regist()"
        >完成</div>
      </div>

    </div>
  </div>
</body>
<script src="../script/api.js"></script>
<script src="../script/zepto.js"></script>
<script src="../script/common.js"></script>
<script>
    $("#step2").hide();
    var count = 0; //间隔函数，1秒执行
    var InterValObj; //timer变量，控制时间
    var icode = '';

    function regist(){
        if ($('#tele').val() == '') {
            alert('请输入手机号');
            return;
        }
        if ($('#pwd1').val() == '') {
            alert('请输入密码');
            return;
        }
        if ($('#pwd1').val().length < 6) {
            alert('密码长度不能小于6位');
            return;
        }
        if ($('#pwd1').val() != $('#pwd2').val()) {
            alert('密码验证不一致');
            return;
        }
        if ($('#code').val() == '') {
            alert('请输入手机验证码');
            return;
        }
        if ($('#name').val() == '') {
            alert('请输入姓名');
            return;
        }
        if ($('#idCard').val() == '') {
            alert('请输入身份证号');
            return;
        }
        if ($('#idCard').val().length != 18) {
            alert('身份证号长度不正确');
            return;
        }
        var d = {
            tele : $('#tele').val(),
            password: $('#pwd1').val(),
            code: $('#code').val(),
            name: $('#name').val(),
            idCard: $('#idCard').val()
        }

        $.ajax({
            url : "http://www.aiyi5.com/doReg",
            type : "POST",
            data : d,
            async : false,
            dataType: 'json',
            error : function(errorMsg) {

            },
            success : function(data) {
                alert(data.msg);
                if(data.code == '0'){
                  openWin('login');
                }
            }
        });
    }

    function getRegCode() {
        if(count > 0){
            return;
        }
        var tele = $('#tele').val();
        if (!(tele.length == 11 && /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|)+\d{8})$/
                .test(tele))) {
            alert('请输入正确的手机号');
            return;
        }

        var d = {
            tele : tele
        }

        $.ajax({
            url : "http://www.aiyi5.com/getRegCode",
            type : "POST",
            data : d,
            async : false,
            dataType: 'json',
            error : function(errorMsg) {

            },
            success : function(data) {
                count = 60;
                if (data.code == '0') {
                    $("#smsCode").val(count + "秒");
                    InterValObj = window.setInterval(SetRemainTime, 1000);
                } else {
                    alert('发送失败');
                }
            }
        });
    }

    function SetRemainTime() {
        if (count == 0) {
            window.clearInterval(InterValObj);//停止计时器
            $("#smsCode").val("获取验证码");
        } else {
            count--;
            $("#smsCode").val(count + "秒");
        }
    }

    function next(){
        if ($('#tele').val() == '') {
            alert('请输入手机号');
            return;
        }
        if ($('#pwd1').val() == '') {
            alert('请输入密码');
            return;
        }
        if ($('#pwd1').val().length < 6) {
            alert('密码长度不能小于6位');
            return;
        }
        if ($('#pwd1').val() != $('#pwd2').val()) {
            alert('密码验证不一致');
            return;
        }
        if(!$('#agree').prop('checked')){
            alert('请阅读并同意《爱易物平台服务协议》');
            return;
        }
        var d = {
            code : $('#code').val()
        }
        $.ajax({
            url : "http://www.aiyi5.com/checkRegCode",
            type : "POST",
            data : d,
            async : false,
            dataType: 'json',
            error : function(errorMsg) {

            },
            success : function(data) {
                if (data.code == '0') {
                    $("#step1").hide();
                    $("#step2").show();
                } else {
                    alert(data.msg);
                }
            }
        });
    }
</script>
</html>
